<?php
/**
 * 文章
 * @author pirate
 */
class Admin_ArticleController extends Edm_Controller_Action {

    public function indexAction () {
        $filter = new Zend_Filter_HtmlEntities();
        $this->view->menu = array( '添加文章' => '/admin/article/add' );
        $this->view->ur_here = '文章列表';
        $this->view->title = '文章列表';
        $this->view->catlist = $this->_articlecat->getArticleCat();
        
        $data = array(
            'pageSize' => $this->common->defaultPageSize,
            'pageNum' => $filter->filter($this->_request->get('page', 1)),
            'cat_id' => $filter->filter($this->_request->get('cat_id', null)),
            'keywords' => $filter->filter($this->_request->get('keywords', null))
        );
        $res = $this->_article->articleList($data);

        $this->view->search = array('cat_id' => $data['cat_id'], 'keywords' => $data['keywords']);
        $this->view->lists = $res['list'];
        $this->pager->Pager($res['count'], $data['pageSize']);
        $this->view->pageResult = $this->pager->display();
        $this->view->url = $this->common->getUrl();
    }

    /* 添加、编辑 */
    public function addAction () {
        $filter = new Zend_Filter_HtmlEntities();
        $act = $filter->filter($this->_request->getParam('act'));

        /* 编辑 获取值 */
        if ($act == 'edit') {
            $article_id = $filter->filter($this->_request->get('article_id'));
            $uri = $filter->filter($this->_request->get('fromurl'));
            $article = $this->_article->getArticle($article_id);
            if ($article !== NULL) {
                $article['uri'] = $uri;
                $article['act'] = 'save';
                $this->view->article = $article;
            } else {
                $this->common->msg($uri, '此文章不存在！', 'Error');
            }
        }

        if ($this->_request->isPost()) {
            $data = array(
                    'title' => $filter->filter($this->_request->getPost('title')),
                    'cat_id' => $filter->filter($this->_request->getPost('cat_id')),
                    'email' => $filter->filter($this->_request->getPost('email')),
                    'author' => $filter->filter($this->_request->getPost('author')),
                    'keywords' => $filter->filter($this->_request->getPost('keywords')),
                    'content' => $filter->filter($this->_request->getPost('content')),
                    'admin_id' => $this->view->identity->admin_id
            );

            $act = $filter->filter($this->_request->getPost('act'));
            $uri = $filter->filter($this->_request->getPost('uri'));
            $thisUrl = $filter->filter($this->_request->getPost('this_url'));

            /* 添加 */
            if (!$act) {
                /* 检查当前类别、标题是否存在 */
                if (!$this->_article->isExists(array('cat_id' => $data['cat_id'], 'title' => $data['title']))) {
                    $this->_article->Insert($data) ? $this->common->msg($thisUrl, '信息添加成功！') : $this->common->msg($thisUrl, '信息添加异常，请稍候再试！', 'Error');
                } else {
                    $this->common->msg($uri, '信息添加异常，请稍候再试！', 'Error');
                }
            } elseif ($act == 'save') {
                $article_id = $filter->filter($this->_request->getPost('article_id'));
                if (is_numeric($article_id)) {
                    if (!$this->_article->isExists(array('article_id' => $article_id, 'cat_id' => $data['cat_id'], 'title' => $data['title']))) {
                        $this->_article->Update($data, $article_id);
                        $this->common->msg($uri, '信息更新成功！');
                    } else {
                        $this->common->msg($thisUrl, '此分类下同标题的文章已经存在', 'Error');
                    }
                }
            }
        }

        $this->view->headScript()->appendFile('/js/fckeditor/fckeditor.js');
        $this->view->catlist = $this->_articlecat->getArticleCat();
        $fck = new Edm_Controller_Plugin_FCKeditor('content');
        $fck->BasePath = '/js/fckeditor/';
        $fck->Height = '260';
        $fck->Value = isset($article['content']) ? htmlspecialchars_decode($article['content']) : '';
        $this->view->fck = $fck;
        $this->view->uri = isset($article['uri']) ? $article['uri'] : $this->common->getUrl();
        $this->view->thisUrl = $this->common->getUrl();
        $this->view->menu = array( '文章列表' => '/admin/article/' );
        $this->view->ur_here = '文章添加/编辑';
        
    }

    /* 删除 */
    public function dropAction () {
        /* 禁用视图 */
        $this->_helper->viewRenderer->setNoRender();
        $filter = new Zend_Filter_HtmlEntities();
        if ($this->_request->isPost()) {
            $article_ids = $this->_request->getPost('article_ids');
            $url = $this->_request->getPost('url');
            $this->_article->Delete($article_ids) ? $this->common->msg($url, '信息删除成功！') : $this->common->msg($url, '信息删除时出现异常,删除失败！', 'Error');
        }
      
    }

}
